﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CombineProto
{
    public class Message
    {
        public string name;
        public List<MessageValue> dots = new List<MessageValue>();

        public override string ToString()
        {
            string ret = string.Empty;
            for (int i = 0; i < dots.Count; i++)
            {
                ret += "\n" + dots[i].ToString();
            }
            return "message " + name + " {" + ret + "\n}";
        }
        public void RemoveGreater(int value)
        {
            for (int i = 0; i < dots.Count; i++)
            {
                int par = int.Parse(dots[i].value);
                if (par > value)
                {
                    dots.RemoveAt(i);
                    i--;
                }
            }
        }
        public void RemoveLower(int value)
        {
            for (int i = 0; i < dots.Count; i++)
            {
                int par = int.Parse(dots[i].value);
                if (par < value)
                {
                    dots.RemoveAt(i);
                    i--;
                }
            }
        }
        public string ToLuaCmd()
        {
            string ret = "local p={\n";
            for (int i = 0; i < dots.Count; i++)
            {
                var dot = dots[i];
                ret += "[" +(i+1) + "] = package..\"." + dot.type + "\"";
                if (i < dots.Count - 1)
                {
                    ret += ",\n";
                }
            }
            ret += "\n}\nreturn {\n  id={\n";
            for (int i = 0; i < dots.Count; i++)
            {
                var dot = dots[i];
                ret += "[" + dot.value + "] = p[" + (i+1) + "]";
                if (i < dots.Count - 1)
                {
                    ret += ",\n";
                }
            }
            ret += "\n},\n  msg={\n";

            for (int i = 0; i < dots.Count; i++)
            {
                var dot = dots[i];
                ret += "[\"" + dot.type + "\"] = p[" + (i + 1) + "]";
                if (i < dots.Count - 1)
                {
                    ret += ",\n";
                }
            }
            ret += "\n},\n msgToId={\n";

            for (int i = 0; i < dots.Count; i++)
            {
                var dot = dots[i];
                ret += "[\"" + dot.type + "\"] = " + dot.value + "";
                if (i < dots.Count - 1)
                {
                    ret += ",\n";
                }
            }
            ret += "\n}\n}";
            return ret;
        }

        public string ToLuaAnnotation()
        {
            var sbRet = new StringBuilder();
            sbRet.AppendFormat("---@class {0}\n", name);
            for (int i = 0; i < dots.Count; i++)
            {
                sbRet.AppendFormat("---@field public {0} number\n", dots[i].name);
            }
            return sbRet.ToString();
        }
    }
}
